package org.dydl.controller.system;

import org.dydl.common.dto.ResponseMessage;
import org.dydl.model.system.Area;
import org.dydl.model.system.Loss;
import org.dydl.model.system.Lossdetail;
import org.dydl.service.system.IGoodsService;
import org.dydl.service.system.LossService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;


/**
 * 耗损关系接口
 * @author 张华城
 *
 */
@RestController
@RequestMapping("/api/loss")
public class LossController {
	
	@Autowired
	private LossService lossService;
	
	@Autowired
	private IGoodsService goodsService;
	
	/**
	 * 耗损关系列表、表头
	 * @param page
	 * @param rows
	 * @param name
	 * @return
	 */
	@RequestMapping("/list")
	public ResponseMessage list(int page, int rows, String name){
		
		ResponseMessage res = lossService.list(page, rows, name);
		
		return res;
	}
	
	/**
	 * 新增、编辑
	 * @param loss
	 * @return
	 */
	@RequestMapping("/save")
	public ResponseMessage save(@RequestBody Area loss){

		ResponseMessage res = lossService.save(loss);

		return res;
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public boolean delete(int[] ids){
		if (ids.length != 0) {
			for (int i = 0; i < ids.length; i++) {
				lossService.delete(ids[i]);
			}
			return true;
		} else {
			return false;
		}
	}

	
	@RequestMapping("/saveMaterial")
	public void saveMaterial(@RequestBody List<Lossdetail> lossdetail){
		lossService.saveMaterial(lossdetail);

	}
	
	/**
	 * 生成uuid
	 * @return
	 */
	@RequestMapping("/getUuid")
	public String getUuid(){
		
		String uuid = UUID.randomUUID().toString();
		
		return uuid;
		
	}
	
	@RequestMapping("/findByUuid")
	public Loss findByUuid(@RequestParam(value = "uuid", required = false)String uuid){
		Loss loss = lossService.findByUuid(uuid);
		return loss;
	}
	@RequestMapping("/findBomByBomNo")
	public ResponseMessage findBomByBomNo(@RequestParam (value = "bomNo" ,required = false) String bomNo){
		return lossService.getBomBybomNo(bomNo);
	}
	
	@RequestMapping("/findByBomNo")
	public List<Lossdetail> findByBomNo(@RequestParam(value = "bomNo", required = false) String bomNo){
		
		List<Lossdetail> list = lossService.findByBomNo(bomNo);
		
		return list;
	}
	
	
	@RequestMapping("/getBomNo")
	public String getBomNo(){
			
		int ran = (int)(Math.random()*90+10);
		String bomNo = "A"+(System.currentTimeMillis())+""+ran;
		return bomNo;
	}

}
